package com.budwk.app.waterquality.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.models.WaterQualityIndicator;
import com.budwk.app.waterquality.services.WaterQualityIndicatorService;
import com.budwk.app.waterquality.vo.ParamProblemVo;
import com.budwk.app.waterquality.vo.WaterQualityCommonVo;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@IocBean
@At("/waterquality/admin/water/quality/indicator")
@ApiDefinition(tag = "水质指标表")
@Slf4j
public class WaterQualityIndicatorController {
    @Inject
    private WaterQualityIndicatorService waterQualityIndicatorService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询水质指标表")
    @ApiResponses(implementation = WaterQualityIndicator.class)
    public Result<?> list(@Param("..") WaterQualityCommonVo reqVo) {
        log.info("WaterqualityindicatorController list param: {}", JSONObject.toJSONString(reqVo));
        return Result.data(waterQualityIndicatorService.listByPage(reqVo));
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "新增水质指标表")
    @ApiOperation(name = "新增水质指标表")
    @ApiResponses
//    @SaCheckLogin
    public Result<?> create(@Param("..")WaterQualityIndicator indicator) {
        log.info("WaterqualityindicatorController create param: {}", JSONObject.toJSONString(indicator));
        WaterQualityIndicator res = waterQualityIndicatorService.addIndicator(indicator);
        return Result.data(res);
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "修改水质指标表")
    @ApiOperation(name = "修改水质指标表")
    @ApiResponses
    @SaCheckLogin
    public Result<?> update(@Param("..")WaterQualityIndicator wkWaterQualityIndicator, HttpServletRequest req) {
        wkWaterQualityIndicator.setUpdateId(SecurityUtil.getUserId());
        waterQualityIndicatorService.updateIgnoreNull(wkWaterQualityIndicator);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @SLog(value = "获取水质指标表")
    @ApiOperation(name = "获取水质指标表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("waterquality.water.quality.indicator")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        WaterQualityIndicator wkWaterQualityIndicator = waterQualityIndicatorService.fetch(id);
        if (wkWaterQualityIndicator == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wkWaterQualityIndicator);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "异常标记")
    @ApiResponses
//    @SaCheckLogin
    public Result<?> abnormalMarking(@Param("..") ParamProblemVo problemVo) {
        log.info("WaterqualityindicatorController abnormalMarking param: {}", JSONObject.toJSONString(problemVo));
        waterQualityIndicatorService.abnormalMarking(problemVo);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "统计报表下载")
    public Result<?> downStatReport(@Param("..") WaterQualityCommonVo reqVo, HttpServletResponse res) {
        log.info("WaterqualityindicatorController downPdfTemplate start");
        try {
            String url = waterQualityIndicatorService.downStatReport(reqVo, res);
            log.info("WaterqualityindicatorController downPdfTemplate end: {}", url);
            return Result.data(url);
        } catch (IOException e) {
            log.error("downStatReport err: ", e);
            throw new AppException("下载异常！");
        }
    }

}
